// components/order-list/order-list.js
let app = getApp()
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        status: Number
    },
    /**
     * 组件的初始数据
     */
    data: {
        orderData: null,
        totalPrice: 0,
        count: 0,
        state: null
    },
    /**
     * 组件的方法列表
     */
    lifetimes: {
        ready() {
            let token = wx.getStorageSync('token')
            wx.request({
                url: 'http://www.kangliuyong.com:10002/findOrder',
                data: {
                    appkey: app.globalData.appkey,
                    tokenString: token,
                    status: this.properties.status
                },
                success: res => {
                    console.log(res);
                    this.getOrder(res.data.result)
                }
            })
        }
    },
    methods: {
        // 处理数据
        getOrder(data) {
            var map = {};
            var arr = [];
            for (let i = 0; i < data.length; i++) {
                var ai = data[i]
                if (!map[ai.oid]) {
                    arr.unshift({
                        oid: ai.oid,
                        state: ai.status,
                        data: [ai]
                    })
                    map[ai.oid] = ai
                } else {
                    for (let j = 0; j < arr.length; j++) {
                        var dj = arr[j]
                        if (dj.oid == ai.oid) {
                            dj.data.unshift(ai)
                            break;
                        }
                    }
                }
            }
            // this.getTotalPrice(arr)
            this.setData({
                orderData: arr
            })
        },
        // 确认收获
        confirm(e) {
            let token = wx.getStorageSync('token')
            let oid = e.currentTarget.dataset.oid
            let index = e.currentTarget.dataset.index
            let orderData = this.data.orderData
            console.log(index);
            wx.request({
                url: 'http://www.kangliuyong.com:10002/receive',
                method: 'POST',
                header: {
                    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                },
                data: {
                    appkey: app.globalData.appkey,
                    tokenString: token,
                    oid: oid
                },
                success: res => {
                    console.log(res);
                    if (res.data.msg == "已收货成功") {
                        wx.showToast({
                            title: '收获成功',
                        })
                        orderData.splice(index, 1)
                        this.setData({
                            orderData: orderData
                        })
                    }
                }
            })
        },
        // 删除订单
        del(e) {
            let token = wx.getStorageSync('token')
            let oid = e.currentTarget.dataset.oid
            let index = e.currentTarget.dataset.index
            let orderData = this.data.orderData
            wx.request({
                url: 'http://www.kangliuyong.com:10002/removeOrder',
                method: 'POST',
                header: {
                    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                },
                data: {
                    appkey: app.globalData.appkey,
                    tokenString: token,
                    oid: oid
                },
                success: res => {
                    console.log(res);
                    if (res.data.msg == "删除订单成功") {
                        wx.showToast({
                            title: '删除成功',
                        })
                        orderData.splice(index, 1)
                        this.setData({
                            orderData: orderData
                        })
                    }
                }
            })
        },
        // getTotalPrice(arr) {
        //     // console.log(arr);
        //     let total = 0
        //     let count = 0
        //     let tlist = []
        //     let clist = []
        //     for (let i = 0; i < arr.length; i++) {
        //         total = arr[i].data.map(item => {
        //             // console.log(item);
        //             return item.count * item.price
        //         })
        //         count = arr[i].data.map(item => {
        //             // console.log(item);
        //             let c = 0
        //             c += item.count
        //             return c
        //         })
        //         tlist.push(total)
        //         clist.push(count)
        //     }
        //     this.setData({
        //         totalPrice: tlist,
        //         count: clist
        //     })
        // }
    },
})